package com.tzy.note.base.thread.state;

/**
 * 测试观察线程状态
 */
public class TestState {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("======");
        });
        //观察状态
        System.out.println(thread.getState()); //New
        //观察启动后
        thread.start(); //启动线程
        System.out.println(thread.getState());//Runable

        //只要线程终止就一直输出线程状态
        while (thread.getState() != Thread.State.TERMINATED){
            Thread.sleep(200);
            System.out.println(thread.getState());//timed_waiting --> terminated
        }
    }
}
